/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import dao.DaoLoginLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import pojo.Usuario;

@ManagedBean
@RequestScoped
public class LoginBean {

    @EJB
    private DaoLoginLocal daoLogin;
    private String usuario;
    private String senha;

    public LoginBean() {
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public void logar(ActionEvent actionEvent) {
        Usuario u = null;
        try {
            u = daoLogin.autenticaUsuario(usuario, senha);
            if (u != null) {
                HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                HttpSession session = req.getSession();

                if (session.getAttribute("nomeUsuario") == null) {

                    session.setAttribute("nomeUsuario", u.getUsuario());
                    try {
                        FacesContext.getCurrentInstance().getExternalContext().redirect("principal.xhtml");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Usuário já está na sessão.", null));

                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Credenciais incorretas.", null));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro desconhecido: " + ex.getMessage(), null));

        }
    }

    public void logout(ActionEvent actionEvent) {
        System.out.println("logouttttt");
        try {
            HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = req.getSession();
            session.removeAttribute("nomeUsuario");
            session.invalidate();
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (IOException e) {
            System.out.println("Erro ao fazer logout." + e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao fazer logout. " + e.getMessage(), null));
        }
    }

    public String getValidaUsuarioSessaoPrincipal() {
        try {
            HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = req.getSession();
            if (session.getAttribute("nomeUsuario") == null) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
                return null;
            } else {
                return (String) session.getAttribute("nomeUsuario");
            }
        } catch (IOException e) {
            System.out.println("Erro pegar usuario da sessao." + e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao fazer logout. " + e.getMessage(), null));

        }
        return null;
    }

    public String getValidaUsuarioSessaoPaginas() {
        try {
            HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = req.getSession();
            if (session.getAttribute("nomeUsuario") == null) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("../index.xhtml");
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao fazer logout. ", null));

                return null;
            } else {
                return null;
            }
        } catch (IOException e) {
            System.out.println("Erro pegar usuario da sessao." + e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao fazer logout. " + e.getMessage(), null));

        }
        return null;
    }

    public String getValidaUsuarioSessaoIndex() {
        try {
            HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = req.getSession();
            if (session.getAttribute("nomeUsuario") != null) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("principal.xhtml");
                return null;
            } else {
                return null;
            }
        } catch (IOException e) {
            System.out.println("Erro pegar usuario da sessao." + e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro ao fazer logout. " + e.getMessage(), null));

        }
        return null;
    }
}
